﻿using System;
using System.Linq;
using System.Web.Services;

namespace MenusHunter.Services
{
    /// <summary>
    /// Summary description for AccountsManager
    /// </summary>
    [WebService(Namespace = "http://menuhunter.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class AccountsManager : System.Web.Services.WebService
    {

        [WebMethod]
        public Entities.Account CheckLogin(string email, string pass)
        {
            using (var db = new MenusHunterDataContext())
            {
                Entities.Account item = (from i in db.Accounts
                            where i.Email == email && i.Password == pass && i.Status == 1
                            select new Entities.Account()
                                       {
                                           Id = i.Id,
                                           Email = i.Email,
                                           FullName = i.FullName,
                                           Phone = i.Phone,
                                           Address = i.Address,
                                           Gender = (bool) i.Gender,
                                           AccessToken = i.AccessToken,
                                           Avatar = i.Avatar
                                       }).SingleOrDefault();

                return item;
            }
        }


        [WebMethod]
        public Entities.Account GetAccountByEmail(string email)
        {
            using (var db = new MenusHunterDataContext())
            {
                Entities.Account item = (from i in db.Accounts
                                         where i.Email==email && i.Status==1
                                         select new Entities.Account()
                                         {
                                             Id = i.Id,
                                             Email = i.Email,
                                             FullName = i.FullName,
                                             Phone = i.Phone,
                                             Address = i.Address,
                                             Gender = (bool)i.Gender,
                                             AccessToken = i.AccessToken,
                                             Avatar = i.Avatar
                                         }).SingleOrDefault();

                return item;
            }
        }

        [WebMethod]
        public Entities.Account GetAccountById(int id)
        {
            using (var db = new MenusHunterDataContext())
            {
                Entities.Account item = (from i in db.Accounts
                                         where i.Id==id && i.Status == 1
                                         select new Entities.Account()
                                         {
                                             Id = i.Id,
                                             Email = i.Email,
                                             FullName = i.FullName,
                                             Phone = i.Phone,
                                             Address = i.Address,
                                             Gender = (bool)i.Gender,
                                             AccessToken = i.AccessToken,
                                             Avatar = i.Avatar
                                         }).SingleOrDefault();

                return item;
            }
        }

        [WebMethod]
        public int NewAccount(string email,string pass,string fullName,string avatar,bool gender,string address, string phone, DateTime birthday,string accesToken,int type,int status)
        {
            using (var db = new MenusHunterDataContext())
            {
                var item = new Account()
                               {
                                   Email = email,
                                   Password = pass,
                                   FullName = fullName,
                                   Gender = gender,
                                   Address = address,
                                   Phone = phone,
                                   AccessToken = accesToken,
                                   Birthday = birthday,
                                   Avatar = avatar,
                                   DateCreate = DateTime.Now,
                                   Type = type,
                                   Status = status
                               };

                db.Accounts.InsertOnSubmit(item);
                db.SubmitChanges();
                return item.Id;
            }
        }
    }
}
